package com.imoblife.brainwavestus.oldMedia;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.customview.SeekBar;
import com.imoblife.brainwavestus.listener.OnSeekBarChangeListener;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class XlargerSetVolum {

	Activity mActivity;

	public XlargerSetVolum(Activity activity) {
		mActivity = activity;
		initVolumSeekBar();
	}

	public void setSystemVolumSize(int size) {

		SharedPreferences preferences = mActivity.getSharedPreferences(
				"Volume", 0);
		preferences.edit().putInt("VolumeSize", size).commit();

		AudioManager audioManager = (AudioManager) mActivity
				.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, size, 0);
	}

	public int getCurrentSystemVolumSize() {
		SharedPreferences preferences = mActivity.getSharedPreferences(
				"Volume", 0);

		return preferences.getInt("VolumeSize",
				(int) (getMaxSystemVolumSize() * 0.8));
	}

	public int getMaxSystemVolumSize() {

		AudioManager audioManager = (AudioManager) mActivity
				.getSystemService(Context.AUDIO_SERVICE);
		return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	}

	private void initVolumSeekBar() {

		final SeekBar seekBar = (SeekBar) mActivity
				.findViewById(R.id.volum_progress);

		seekBar.setMax(getMaxSystemVolumSize());

		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar VerticalSeekBar) {
				

			}

			public void onStartTrackingTouch(SeekBar VerticalSeekBar) {
				

			}

			public void onProgressChanged(SeekBar verticalSeekBar,
					int progress, boolean fromUser) {
				setSystemVolumSize(verticalSeekBar.getMax() - progress);
			}
		});
		seekBar.setProgress(seekBar.getMax() - getCurrentSystemVolumSize());

		ImageView top = (ImageView) mActivity.findViewById(R.id.volum_top);

		ImageView buttom = (ImageView) mActivity
				.findViewById(R.id.volum_buttom);
		top.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				if (seekBar.getProgress() > 0) {
					seekBar.setProgress(seekBar.getProgress() - 1);
				}
			}
		});

		buttom.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (seekBar.getProgress() < seekBar.getMax()) {
					seekBar.setProgress(seekBar.getProgress() + 1);
				}
			}
		});

	}
}
